package com.geeguo.ebuilder.client.business.system.excelimport.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geeguo.ebuilder.client.base.core.utils.FileHelper;
import com.geeguo.ebuilder.client.base.database.annotation.TDS;
import com.geeguo.ebuilder.client.base.tenant.model.DataContext;
import com.geeguo.ebuilder.client.business.system.excelimport.mapper.ExcelImportMapper;
import com.geeguo.ebuilder.client.business.system.excelimport.model.ExcelImportConverter;
import com.geeguo.ebuilder.client.business.system.excelimport.model.ExcelImportEntity;
import com.geeguo.ebuilder.client.business.system.excelimport.model.ExcelImportQuery;
import com.geeguo.ebuilder.client.business.system.excelimport.model.ExcelImportVO;
import com.geeguo.ebuilder.client.business.system.excelimport.service.ExcelImportService;
import com.geeguo.ebuilder.client.business.system.excelimportfailitem.service.ExcelImportFailItemService;
import com.geeguo.ebuilder.core.common.exception.ApplicationException;
import com.geeguo.ebuilder.core.common.model.vo.PageListVO;
import com.geeguo.ebuilder.core.common.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
public class ExcelImportServiceImpl implements ExcelImportService {

    @Autowired
    private ExcelImportMapper excelImportMapper;
    @Autowired
    private ExcelImportFailItemService excelImportFailItemService;

    @TDS
    @Override
    public PageListVO<ExcelImportVO> list(ExcelImportQuery query, DataContext dataContext) {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotEmpty(query.getUserId())) {
            params.put("userId", query.getUserId());
        }
        if (query.getStatus() != null && query.getStatus() > 0) {
            params.put("status", query.getStatus());
        }
        params.put("tenantCode", dataContext.getTenantCode());
        IPage<ExcelImportEntity> page = excelImportMapper.list(new Page<>(query.getPageNo(), query.getPageSize()), params);
        List<ExcelImportVO> result = new ArrayList<>();
        if (page.getRecords() != null && !page.getRecords().isEmpty()) {
            for (ExcelImportEntity item : page.getRecords()) {
                result.add(ExcelImportConverter.INSTANCE.convert2VO(item));
            }
        }
        return PageUtils.getPageListVO(page, result);
    }

    @TDS
    @Override
    public ExcelImportVO get(String id, DataContext dataContext) {
        ExcelImportEntity result = excelImportMapper.selectById(id);
        if (result != null) {
            return ExcelImportConverter.INSTANCE.convert2VO(result);
        }
        return null;
    }

    @TDS
    @Override
    public Integer countUnfinished(String userId, DataContext dataContext) {
        return excelImportMapper.countUnfinished(userId);
    }

    @TDS
    @Override
    public boolean save(ExcelImportEntity entity, DataContext dataContext) {
        return excelImportMapper.insert(entity) > 0;
    }

    @TDS
    @Override
    public boolean updatePending(String id, DataContext dataContext) {
        return excelImportMapper.updatePending(id) == 1;
    }

    @TDS
    @Override
    public boolean updateProcessing(String id, Date startTime, DataContext dataContext) {
        return excelImportMapper.updateProcessing(id, startTime) == 1;
    }

    @TDS
    @Override
    public boolean updateFinished(String id, Date endTime, DataContext dataContext) {
        return excelImportMapper.updateFinished(id, endTime) == 1;
    }

    @TDS
    @Override
    public boolean updateError(String id, String errorMessage, DataContext dataContext) {
        return excelImportMapper.updateError(id, errorMessage) == 1;
    }

    @TDS
    @Override
    public boolean updateCount(String id, Integer successCount, Integer failCount, DataContext dataContext) {
        return excelImportMapper.updateCount(id, successCount, failCount) == 1;
    }

    @TDS
    @Override
    @Transactional
    public boolean remove(String id, DataContext dataContext) {
        boolean result = true;
        ExcelImportVO excelImport = this.get(id, dataContext);
        if (excelImport != null) {
            result = excelImportFailItemService.removeByImportId(id, dataContext);
            if (result) {
                result = excelImportMapper.deleteById(id) == 1;
            }
            if (result) {
                result = FileHelper.remove(excelImport.getFilePath());
            }
            if (!result) {
                throw new ApplicationException("Excel导入任务删除失败！");
            }
        }
        return result;
    }
}
